/*           Copyright(c) 2005-2008 Dave Sexton            *
 *                   HtmlEditor Control                    *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DaveSexton.Controls
{
	public partial class HtmlTableForm : Form
	{
		#region Public Properties
		public int SelectedColumnCount
		{
			get
			{
				return (int) nColumns.Value;
			}
			set
			{
				nColumns.Value = value;
			}
		}

		public int SelectedRowCount
		{
			get
			{
				return (int) nRows.Value;
			}
			set
			{
				nRows.Value = value;
			}
		}
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="HtmlTableForm" /> class.
		/// </summary>
		public HtmlTableForm()
		{
			InitializeComponent();
		}
		#endregion

		#region Methods
		private void DrawPreview(Graphics graphics)
		{
			int cols = SelectedColumnCount;
			int rows = SelectedRowCount;

			int height = preview.ClientRectangle.Height;
			int width = preview.ClientRectangle.Width;

			int colSpacing = width / cols;
			int rowSpacing = height / rows;

			int nVerticalLines = cols - 1;
			int nHorizontalLines = rows - 1;

			for (int c = 0; c < nVerticalLines; c++)
			{
				int x = (c + 1) * colSpacing;
				graphics.DrawLine(Pens.Black, x, 0, x, height);
			}

			for (int r = 0; r < nHorizontalLines; r++)
			{
				int y = (r + 1) * rowSpacing;
				graphics.DrawLine(Pens.Black, 0, y, width, y);
			}

			TextRenderer.DrawText(graphics, Resources.Text.Preview, Font, preview.ClientRectangle, Color.Gray, Color.Empty, 
				TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine);
		}
		#endregion

		#region Event Handlers
		private void numericUpDown_ValueChanged(object sender, EventArgs e)
		{
			preview.Invalidate();
		}

		private void panel1_Paint(object sender, PaintEventArgs e)
		{
			DrawPreview(e.Graphics);
		}
		#endregion
	}
}